package com.twt.service;

import com.twt.Dao.MyLakeDao;
import com.twt.entity.Result;
import com.twt.entity.User;
import com.twt.Dao.UserDao;
import com.twt.entity.lake;
import com.twt.entity.picked;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.UUID;

@Service
@Transactional(rollbackFor = RuntimeException.class)
public class UserService {

    @Autowired
    private UserDao userMapper;

    /**
     * 登录
     *
     * @param username,password 用户名和密码
     * @return Result
     */
    public Result login(String username, String password) {
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        user.setNickname(null);
        user.setHead(null);
        user.setTelephone(null);
        user.setFirstHead(0);
        Result result = new Result();
        result.setSuccess(false);
        result.setDetail(null);
        Long userId = userMapper.login(username, password);
        user.setId(userId);
        try {
            if (userId == null) {
                result.setMsg("用户名或密码错误");
            } else {
                if (userMapper.first(userId) == null)//初次登录
                {
                    result.setMsg("登录成功,初次登录,需要设置昵称和头像");
                    result.setSuccess(true);
                    user.setId(userId);
                } else {
                    result.setMsg("登录成功，已经设置头像和昵称");
                    result.setSuccess(true);
                    user.setNickname(userMapper.first(userId));
                    user.setHead(userMapper.findHead(userId));
                    if(user.getHead() != null)
                        user.setHead("http://60.205.223.228:8080/images/"+user.getHead());
                    user.setFirstHead(userMapper.findFirstHead(userId));
                }
                result.setDetail(user);
            }
        } catch (Exception e) {
            result.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return result;
    }


    //计算数据表数据数量
    private Long countUserNumber() {
        return userMapper.countUserNumber();
    }

    /**
     * 设置昵称
     *
     * @param username,nickname 用户名和昵称
     * @return Result
     */
    public Result setNickName(String username, String nickname) {
        //如果数据量大，为了节约内存，就会分多次进行遍历
        Result result = new Result();
        List<User> users;//存在这
        int userNumber = 0;//起始id（不包含）
        int oneTimeHowMuch = 10000;//每次的数据量

        while (true) {
            //按量提取数据
            users = userMapper.selectManyUser(userNumber, oneTimeHowMuch);
            //对users进行遍历
            for (User user : users) {
                if (nickname.equals(user.getNickname())) {
                    result.setMsg("有相同昵称了");
                    result.setSuccess(false);
                    return result;
                }
            }

            //调整下一组数据的起始id
            userNumber += oneTimeHowMuch;
            //判断是否已经遍历完全部数据
            //countUserNumber()是数出数据表中数据个数
            if (userNumber > countUserNumber()) {
                //save似乎没什么用，他的值应该是 符合相应条件的数据的个数
                int save = userMapper.setNickName(username, nickname);
                result.setMsg("昵称设置成功");
                result.setSuccess(true);
                return result;
            }
        }
    }

    /**
     * 系统头像上传数据库
     *
     * @param username,firstHead 参数封装
     * @return Result
     */
    public Result setFirstHead(int firstHead, String username) {
        Result result = new Result();//初始化result
        result.setDetail(null);
        result.setSuccess(false);
        result.setMsg(null);
        //写入数据库
        userMapper.setFirstHead(username, firstHead);
        result.setSuccess(true);
        result.setMsg("系统初始头像设置完成");
        return result;
    }


    public Result getAllBottle(String username) {
        Result result = new Result();
        List<picked> one = userMapper.getAllBottleP(username);
        one.addAll(userMapper.getAllBottleT(username));
        result.setDetail(one);
        result.setSuccess(true);
        result.setMsg("yes");
        return result;
    }


}
